package com.xiyu.service.service.unit;

import com.xiyu.service.vo.unit.worker.WorkerGetGroupRankInfoOutput;
import com.xiyu.service.vo.unit.worker.WorkerUpdateInput;
import com.xiyu.service.vo.unit.worker.WorkerPageQueryOutput;
import com.xiyu.service.vo.unit.worker.WorkerPageQueryInput;
import com.xiyu.service.vo.unit.worker.WorkerCreateInput;
import com.xiyu.service.vo.unit.worker.WorkerSingleGetOutput;
import com.xiyu.service.model.unit.worker.UnitWorker;
import com.xiyu.service.repository.unit.worker.UnitWorkerRepository;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.unit.WorkerConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.unit.WorkerErrorCode.*;

/**
 * 单位员工 Service 实现类
 */
@Service
@Validated
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private UnitWorkerRepository unitWorkerRepository;




    @Override
    public PageResult<WorkerPageQueryOutput> pageQuery(WorkerPageQueryInput inputVO) {
        Page<UnitWorker> pageOfficeUnitWorker = unitWorkerRepository.pageQuery(inputVO);
        List<WorkerPageQueryOutput> listOfficeUnitWorker = WorkerConvert.INSTANCE.pageQueryOutputConvert(pageOfficeUnitWorker.getRows());
        return new PageResult<>(listOfficeUnitWorker, pageOfficeUnitWorker.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(WorkerCreateInput inputVO) {
        UnitWorker newOfficeUnitWorker = WorkerConvert.INSTANCE.createInputConvert(inputVO);
        newOfficeUnitWorker = unitWorkerRepository.insert(newOfficeUnitWorker);
        return newOfficeUnitWorker.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(WorkerUpdateInput inputVO) {
        Optional<UnitWorker> optionalOldOfficeUnitWorker = unitWorkerRepository.findById(inputVO.getId());
        if(!optionalOldOfficeUnitWorker.isPresent()){
            throw exception(OFFICE_UNIT_WORKER_NOT_EXIST);
        }
        UnitWorker updateOfficeUnitWorker = WorkerConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldOfficeUnitWorker.get(), updateOfficeUnitWorker))
            unitWorkerRepository.update(updateOfficeUnitWorker);
        return true;
    }

    @Override
    public WorkerSingleGetOutput singleGet(Long id) {
        Optional<UnitWorker> optionOfficeUnitWorker = unitWorkerRepository.findById(id);
        if(!optionOfficeUnitWorker.isPresent()){
            throw exception(OFFICE_UNIT_WORKER_NOT_EXIST);
        }
        return WorkerConvert.INSTANCE.singleGetOutputConvert(optionOfficeUnitWorker.get());

    }

    @Override
    public List<WorkerGetGroupRankInfoOutput> getGroupRankInfo() {
        return WorkerConvert.INSTANCE.getGroupRankInfoOutputConvert(unitWorkerRepository.findGroupType());
    }

}
